package com.example.simplecalculator;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

@SuppressLint("NonConstantResourceId")
public class MainActivity extends AppCompatActivity {

    // 算式
    private final StringBuffer details = new StringBuffer();
    // 结果
    private int result = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView detailsView = findViewById(R.id.details);
        TextView resultView = findViewById(R.id.result);

        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sInput = ((Button) v).getText().toString();
                switch (v.getId()) {
                    case R.id.n0:
                        if (details.length() == 0 || details.substring(details.length() - 1).equals("÷")) {
                            break;
                        }
                    case R.id.n1:
                    case R.id.n2:
                    case R.id.n3:
                    case R.id.n4:
                    case R.id.n5:
                    case R.id.n6:
                    case R.id.n7:
                    case R.id.n8:
                    case R.id.n9:
                        details.append(sInput);
                        break;
                    case R.id.button_point:
                        break;
                    case R.id.button_subtract:
                    case R.id.button_add:
                    case R.id.button_multiply:
                    case R.id.button_divide:
                        if (details.length() == 0)
                            break;
                        if ((!details.substring(details.length() - 1).equals("×"))
                                && (!details.substring(details.length() - 1).equals("÷"))
                                && (!details.substring(details.length() - 1).equals("+"))
                                && (!details.substring(details.length() - 1).equals("-"))
                                && (!details.substring(details.length() - 1).equals("."))) {
                            details.append(sInput);
                        } else {
                            details.replace(details.length() - 1, details.length(), sInput);
                        }
                        break;
                    case R.id.button_empty:
                        details.setLength(0);
                        detailsView.setVisibility(View.GONE);
                        break;
                    case R.id.button_back:
                        if (details.substring(details.length() - 1).equals("=")){
                            // 按了等于之后再按删除直接清空
                            details.setLength(0);
                            detailsView.setVisibility(View.GONE);
                        }
                        if (details.length() > 0) {
                            details.deleteCharAt(details.length() - 1);
                        }
                        break;
                    case R.id.button_equalSign:
                        if (details.length() == 0)
                            break;
                        if ((details.substring(details.length() - 1).equals("×"))
                                || (details.substring(details.length() - 1).equals("÷"))
                                || (details.substring(details.length() - 1).equals("+"))
                                || (details.substring(details.length() - 1).equals("-"))
                                || (details.substring(details.length() - 1).equals("."))) {
                            details.replace(details.length() - 1, details.length(), "");
                        }
                        // 计算
                        result = CalculatorUtil.calculation(details.toString());
                        // 显示过程和结果
                        detailsView.setVisibility(View.VISIBLE);
                        detailsView.setText(details.append("="));
                        resultView.setText(String.valueOf(result));
                        Log.d("测试", details.toString() + result);
                        return;
                }
                resultView.setText(details);
            }
        };

        findViewById(R.id.n1).setOnClickListener(onClickListener);
        findViewById(R.id.n2).setOnClickListener(onClickListener);
        findViewById(R.id.n3).setOnClickListener(onClickListener);
        findViewById(R.id.n4).setOnClickListener(onClickListener);
        findViewById(R.id.n5).setOnClickListener(onClickListener);
        findViewById(R.id.n6).setOnClickListener(onClickListener);
        findViewById(R.id.n7).setOnClickListener(onClickListener);
        findViewById(R.id.n8).setOnClickListener(onClickListener);
        findViewById(R.id.n9).setOnClickListener(onClickListener);
        findViewById(R.id.n0).setOnClickListener(onClickListener);
        findViewById(R.id.button_point).setOnClickListener(onClickListener);
        findViewById(R.id.button_add).setOnClickListener(onClickListener);
        findViewById(R.id.button_subtract).setOnClickListener(onClickListener);
        findViewById(R.id.button_multiply).setOnClickListener(onClickListener);
        findViewById(R.id.button_divide).setOnClickListener(onClickListener);
        findViewById(R.id.button_empty).setOnClickListener(onClickListener);
        findViewById(R.id.button_back).setOnClickListener(onClickListener);
        findViewById(R.id.button_equalSign).setOnClickListener(onClickListener);
    }

}
